package com.jiangxjsy.admin.service.impl;

import com.jiangxjsy.admin.mapper.AdminRoleAuthMapper;
import com.jiangxjsy.admin.service.AdminRoleAuthService;
import com.jiangxjsy.admin.service.AdminRoleMenuService;
import com.jiangxjsy.admin.service.AdminUserRoleService;
import com.jiangxjsy.admin.support.AdminUserSupport;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;

/**
 * wukong-crm-master
 * 蒋胜宇
 * 2025/6/6 17:23
 */

@Service
public class AdminRoleAuthServiceImpl implements AdminRoleAuthService {
    @Resource
    private AdminRoleAuthMapper adminRoleAuthMapper;

    @Resource
    private AdminRoleMenuService adminRoleMenuService;

    @Resource
    private AdminUserRoleService adminUserRoleService;

    @Resource
    private AdminUserSupport adminUserSupport;

    /**
     * 是否可以查询全部角色
     *
     * @param userId 用户 id
     * @return 是否可以查询全部角色
     */
    @Override
    public boolean isQueryAllRole(Long userId) {
        List<Integer> userRoleIds = adminUserRoleService.queryUserRoleIds(userId);
        Integer count = adminRoleMenuService.countByMenuIdAndRoleIds(175L, userRoleIds);
        return adminUserSupport.isAdmin(userId) || count > 0;
    }

    /**
     * 查询当前用户的能访问角色列表
     *
     * @param userId 用户 id
     * @return 角色列表
     */
    @Override
    public Set<Integer> queryAuthByUser(Long userId) {
        List<Integer> roles = adminUserRoleService.queryUserRoleIds(userId);
        return adminRoleAuthMapper.selectAuthRoleIdsByRoleIds(roles);
    }
}
